package com.sweet.imserver.model;

import io.vertx.core.json.JsonObject;

public class RespData {

  private int code;

  private String errorMsg;

  private Object data;

  public String toJson() {
    JsonObject object = new JsonObject()
      .put("code", code)
      .put("data", data);
    if (errorMsg != null) {
      object.put("errorMsg", errorMsg);
    }
    return object.encode();
  }

  public RespData(int code, String errorMsg, Object data) {
    this.code = code;
    this.errorMsg = errorMsg;
    this.data = data;
  }

  public static RespData success(Object data) {
    return new RespData(1, null, data);
  }

  public static RespData fail(String errorMsg) {
    return new RespData(0, errorMsg, null);
  }


  public int getCode() {
    return code;
  }

  public void setCode(int code) {
    this.code = code;
  }

  public String getErrorMsg() {
    return errorMsg;
  }

  public void setErrorMsg(String errorMsg) {
    this.errorMsg = errorMsg;
  }

  public Object getData() {
    return data;
  }

  public void setData(Object data) {
    this.data = data;
  }
}
